/*
 * 
 */
package a;

import java.util.List;
import java.util.StringTokenizer;

/**
 * 
 * @author Sillywolf Created on 2008-05-26
 *
 */
public class  NatUtil{

	public static boolean ipListCheck(List ip_list, String client_ip) {

		String start_ip = "";
		String end_ip = "";
		for (int i = 0; i < ip_list.size(); i++) {
			start_ip = ip_list.get(i).toString().split(";")[0];
			end_ip = ip_list.get(i).toString().split(";")[1];
			if (ipCheck(start_ip, end_ip, client_ip)) {
				return true;
			}
			start_ip = "";
			end_ip = "";
		}
		return false;
	}

	public static boolean ipCheck(String start_ip, String end_ip, String client_ip) {

		String start_ip_formated = formatIP(start_ip);
		String end_ip_formated = formatIP(end_ip);
		String client_ip_formated = formatIP(client_ip);

		if (client_ip_formated.compareToIgnoreCase(start_ip_formated) < 0
				|| client_ip_formated.compareToIgnoreCase(end_ip_formated) > 0) {
			return false;
		} else {
			return true;
		}
	}

	public static String formatIP(String ip) {
		String strIP = "";
		String strTemp = "";
		StringTokenizer st = new StringTokenizer(ip, ".");
		while (st.hasMoreTokens()) {
			strTemp = st.nextToken();
			strTemp = ("00" + strTemp.trim());
			strTemp = strTemp.substring(strTemp.length() - 3);
			strIP += "." + strTemp;
		}
		strIP = strIP.substring(1);
		return strIP;
	}
	
	public static String unformatIP(String ip) {
        String strIP = "";
        String strTemp = "";
        StringTokenizer st = new StringTokenizer(ip, ".");
        while (st.hasMoreTokens()) {
            strTemp = st.nextToken();
            strTemp = ""+Integer.parseInt(strTemp.trim());
            strIP += "." + strTemp;
        }
        strIP = strIP.substring(1);
        return strIP;
    }
}
